package com.yxzr.youyou.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yxzr.youyou.R;
import com.yxzr.youyou.adapter.ExchangeRecordAdapter;
import com.yxzr.youyou.adapter.IntegralMallAdapter;
import com.yxzr.youyou.base.BaseFragment;
import com.yxzr.youyou.bean.ExchangeRecordBean;
import com.yxzr.youyou.bean.IntegralMallBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ExchangeRecordFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mRefresh;
    private ExchangeRecordAdapter adapter;
    private SharedPreferencesHelper sp;
    private int currIndex = 1;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_exchangerecord,null);
        sp = new SharedPreferencesHelper(getActivity(),"logindata");
        initView(v);
        return v;
    }

    private void initView(View v) {
        mRecyclerView = v.findViewById(R.id.recyclerView);
        mRefresh = v.findViewById(R.id.exchangerecord_refreshLayout);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration(20,1));
        adapter = new ExchangeRecordAdapter(getContext());
        mRecyclerView.setAdapter(adapter);

        //下拉刷新
        mRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                currIndex = 1;
                initData();
            }
        });
        //上提加载
        mRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                currIndex ++;
                initData();
            }
        });

        initData();
    }

    private void initData(){
        RequestUtil.getInstence().API().exchange_record((String) sp.getSharedPreference("token",""),currIndex+"","10")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(getActivity(),true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        ExchangeRecordBean bean = gson.fromJson(t,ExchangeRecordBean.class);

                        if (currIndex == 1) adapter.clearList();
                        adapter.addList(bean.getData());
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {
                        if (currIndex == 1){
                            mRefresh.finishRefresh();
                        }else {
                            mRefresh.finishLoadMore();
                        }
                    }

                    @Override
                    protected void onErr(Throwable e) {
                        if (currIndex == 1){
                            mRefresh.finishRefresh();
                        }else {
                            mRefresh.finishLoadMore();
                        }
                    }
                });
    }
}
